#include "Common.h"
#include "Camera.h"
#include "SceneManager.h"
#include "Scene.h"

//------------------------------------------
Camera::Camera(const std::string& name, Scene* owner)
	: MovableObject(name, owner)
	, mFovY(PI / 4.0f)
	, mAspect(1.33333333333333f)
	, mNearDistance(20.0f)
	, mFarDistance(MAX_WORLD_SIZE)
	, mPosition(Vector3::Zero)
	, mRotation(Rotator::Identity)
	, mNeedUpdate(true)
{
}
//------------------------------------------
Camera::~Camera()
{
}
//------------------------------------------
void Camera::Render(RenderTarget* renderTarget)
{
	Update();

	mOwner->getCreator()->Render(this, renderTarget);
}
//------------------------------------------
void Camera::NeedUpdate()
{
	if (mNeedUpdate)
		return;
	mNeedUpdate = true;
}
//------------------------------------------
void Camera::Update()
{
	if (mNeedUpdate == false)
		return;

	mViewMatrix = Matrix::TranslationMatrix(-mPosition) * Matrix::InverseRotationMatrix(mRotation);
	mProjectionMatrix = Matrix::PerspectiveMatrix(mFovY, mAspect, mNearDistance, mFarDistance);

	// init the frustum planes.
	mFrustum.Init(mViewMatrix * mProjectionMatrix);

	mNeedUpdate = false;
}
//------------------------------------------
void Camera::Move(const Vector3& move)
{
	mPosition += mRotation.Quat() * move;
	NeedUpdate();
}
//------------------------------------------
void Camera::Rotate(int x, int y, int z)
{
	mRotation += Rotator(x, y, z);
	NeedUpdate();
}
//------------------------------------------